package org.example.springboot3.controller;

import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.springboot3.common.Result;
import org.example.springboot3.entity.Message;
import org.example.springboot3.service.MessageService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/messages")
public class MessageController {

    @Resource
    private MessageService messageService;

    @GetMapping("/selectAll")
    public Result selectAll(Message message) {
        List<Message> list = messageService.selectAll(message);
        return Result.success(list);
    }

    @GetMapping("/selectPage")
    public Result selectPage(Message message,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(required = false) String keyword) {
        message.setKeyword(keyword);
        PageInfo<Message> pageInfo = messageService.selectPage(message, pageNum, pageSize);
        return Result.success(pageInfo);
    }


    @PostMapping("/add")
    public Result addMessage(@RequestBody Message message) {
        messageService.addMessage(message);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id, @RequestParam Integer userId) {
        Message message = messageService.selectById(id);
        if (message != null && message.getUserId().equals(userId)) {
            messageService.deleteById(id);
            return Result.success();
        } else {
            return Result.error("500","你没有权限删除此评论");
        }
    }


    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        messageService.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping("/reply")
    public Result replyMessage(@RequestBody Message message) {
        messageService.replyMessage(message);
        return Result.success();
    }

    @PostMapping("/like/{id}")
    public Result likeMessage(@PathVariable Integer id) {
        messageService.likeMessage(id);
        return Result.success();
    }


    @GetMapping("/selectAllWithHierarchy")
    public Result selectAllWithHierarchy() {
        List<Message> list = messageService.selectAllMessagesWithHierarchy();
        System.out.println(list); // 添加日志
        return Result.success(list);
    }





}//

